import { HttpService } from './../../shared/service/http.service';
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';

@Component({
  selector: 'app-tree-node',
  templateUrl: './tree-node.component.html',
  styleUrls: ['./tree-node.component.scss'],
})
export class TreeNodeComponent implements OnInit {

  @Input() nzTreeNode: any;
  @Input() httpData: any;


  @Output() clickValue = new EventEmitter<any>();

  chilidrenObj = {};

  @Input() value: any; // 选中值

  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService
  ) { }

  ngOnInit() {
  }

  // 展开\折叠树形结构
  onFold(item) {
    if (!item!.isFold) {
      item.isFold = true;
      const params = {
        content: {
        },
        id: this.httpData.id,
        sessionId: localStorage.getItem('sessionId')
      };
      params.content[this.httpData.parentCode] = item.code;
      this.http.post('application/submit', params, (res) => {
        if (res.success) {
          this.chilidrenObj[item.code] = res.data.data;
        } else {
          this.appGlobalService.showToast(res.message, 1500, "top");
        }
      });
    } else {
      item.isFold = false;
    }

  }

  //  选中\取消 部门
  onChecked(item) {

    if (!this.value || this.value.code !== item.code) {
      this.clickValue.emit(item);
      this.value = item;
    } else {
      this.value = null;
      this.clickValue.emit(null);
    }

  }

  onValue(item) {
    if (!item) {
      this.value = null;
      this.clickValue.emit(null);
      return;
    }

    this.value = item;
    this.clickValue.emit(item);
  }

}